#include<stdlib.h>
#include<stdio.h>
#include<math.h>

/*	Calculating the pi value using borwein's algorithm (1984)	*/
/*	http://en.wikipedia.org/wiki/Borwein's_algorithm		*/

int main(int argc, char **argv)
{
	if(argc != 2) {
		printf("Usage: ./borwein-seq <number_of_iteractions>\n");
		exit(1);
	}

	/* 	initializating the variables	*/

	//	number of iteractions
	int n = atoi(argv[1]);	

	//	all terms used by the borwein's method
	double a = sqrt(2), b = 0.0, p = 2.0 + sqrt(2);
	double a1, b1, p1;

	int i;
	for(i = 0; i < n; i++){
		
		//	for each iteraction calculate all new terms
		a1 = (sqrt(a) + 1.0/sqrt(a))/2.0;
		b1 = ((1.0 + b)*sqrt(a))/(a + b);
		p1 = ((1 + a1)*p*b1)/(1 + b1);

		a = a1, b = b1, p = p1;
	}

	printf("%.7lf\n", p);

	return 0;
}
